<template>
  <el-form :inline="true">
    <el-form-item>
      <el-date-picker v-model="queryParam.dateArray"
                      :shortcuts="Dic.DateShortcuts"
                      @change="handleQuery"
                      type="daterange"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      value-format="YYYY-MM-DD"/>
    </el-form-item>
    <el-form-item>
      <el-select v-model="queryParam.cateId" placeholder="选择分类"
                 filterable clearable
                 @change="handleQuery">
        <el-option v-for="item in cateList"
                   :key="item.pfId"
                   :label="item.pfName"
                   :value="item.pfId"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-input v-model="queryParam.keyword"
                @keyup.enter="handleQuery"
                placeholder="商品名称\串号"
                type="text"
                auto-complete="off"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleQuery">搜索</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="tableData.list" v-loading="loading"
            :default-sort="sort"
            @sort-change="sortChange" border height="calc(100vh - 210px)">
    <el-table-column label="商品名称" prop="goodsName" min-width="360"/>
    <el-table-column label="串号" prop="goodsCode" width="180" align="center"/>
    <el-table-column label="销售量" prop="orderNumber" width="100" align="center"/>
    <el-table-column label="赠送量" prop="giftTotal" width="100" align="center"/>
    <el-table-column label="调出量" prop="dhNumber" width="100" align="center" sortable="custom"/>
    <el-table-column label="出货量" prop="ourNumber" width="100" align="center" sortable="custom"/>
    <el-table-column label="库存" prop="stockNumber" width="100" align="center" sortable="custom"
                     class-name="chocolate"/>
    <el-table-column label="零售价" prop="originalPrice" min-width="90" align="right" :formatter="Dic.Price"/>
    <el-table-column label="销售均价" prop="avgOrder" min-width="90" align="right" :formatter="Dic.Price"/>
    <el-table-column label="总均价" prop="avgTotal" min-width="90" align="right" :formatter="Dic.Price"/>
    <el-table-column label="总金额" prop="orderTotal" width="160" align="right" :formatter="Dic.Price"/>
    <el-table-column label="操作" width="120" align="center" fixed="right">
      <template v-slot="{row}">
        <el-button link type="primary" disabled>
          查看
        </el-button>
        <el-button link type="warning" @click="copy(row)">
          复制
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination layout="->,total,sizes,prev,pager,next,jumper"
                 v-model:current-page="queryParam.page"
                 v-model:page-size="queryParam.limit"
                 :total="tableData.total" background
                 @current-change="loadList"
                 @size-change="loadList"/>
</template>

<script lang="ts">
import orderApi from "@/api/order";
import Dic from "@/utils/dic";
import {ElNotification} from 'element-plus'
import partsApi from "@/api/parts";

export default {
  name: "orderPartsStatisticsList",
  data() {
    return {
      Dic,
      queryParam: {
        page: 1,
        limit: 20,
        keyword: "",
        sortName: "",
        sortType: "",
        prop: "ourNumber",
        order: "descending",
        cateId: "",
        dateArray: []
      },
      sort: {
        prop: "ourNumber",
        order: "descending",
      },
      tableData: {
        list: [],
        total: 0,
      },
      cateList: [],
      loading: false,
    };
  },
  created() {
    this.loadCateList();
    this.loadList();
  },
  methods: {
    handleQuery() {
      this.queryParam.page = 1;
      this.loadList();
    },
    loadList() {
      this.loading = true;
      orderApi.statisticsByParts(this.queryParam).then(res => {
        this.loading = false;
        this.tableData = res.data
      })
    },
    sortChange({prop, order}) {
      this.sort.prop = prop
      this.sort.order = order
      let sortType = Dic.Sort[order];
      if (sortType) {
        this.queryParam.sortName = prop
        this.queryParam.sortType = sortType
      }
      this.loadList()
    },
    copy({goodsName, orderNumber, stockNumber}) {
      let copyContent = `${goodsName} \n销量：${orderNumber} 库存：${stockNumber}`;
      copyContent += ` 补货量：${orderNumber - stockNumber}`
      navigator.clipboard.writeText(copyContent);
      ElNotification.success({
        title: '复制成功',
        message: copyContent,
      })
    },
    loadCateList() {
      partsApi.categoryList({byLan: true, status: 1, limit: 100}).then(res => {
        this.cateList = res.data.list
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.el-form {
  --el-date-editor-daterange-width: 200px;
}

.el-select {
  width: 140px;
}

</style>